package com.hanshushi_jiekou;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ComparatorDemo {
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("cccc");
        strings.add("aa");
        strings.add("b");
        strings.add("ddd");
        System.out.println("排序前：" + strings);  // 排序前：[cccc, aa, b, ddd]
//        Collections.sort(strings);
        Collections.sort(strings, getComparator());
        System.out.println("排序后：" + strings);// 排序后：[b, aa, ddd, cccc]

    }

    private static Comparator<String> getComparator() {
//        return new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                return o1.length() - o2.length();
//            }
//        };

        return (s1, s2) -> s1.length() - s2.length();
    }
}
